package com.qianfeng.biquge.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.biquge.pojo.Books;
import com.qianfeng.biquge.service.BookService;
import com.qianfeng.biquge.vo.BooksVo;
import com.qianfeng.biquge.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping("/newbook")
    public R addBook(@RequestBody Books books){

        Books book = bookService.addBook(books);

        return R.setOk(book);

    }

    @GetMapping("/books/{name}/{pageNum}/{pageSize}")
    public R getAllBookByBookNameOrByAuthorName(@PathVariable String name,@PathVariable int pageNum,@PathVariable int pageSize) throws UnsupportedEncodingException {

        PageInfo<BooksVo> pageInfo = bookService.getAllBookByBookNameOrByAuthorName(name, pageNum, pageSize);

        return R.setOk(pageInfo);

    }

    @DeleteMapping("/delete/{bookId}")
    public R deleteBook(@PathVariable Long bookId){

        int i = bookService.deleteBook(bookId);

        return R.setOk(i);

    }

    @GetMapping("/books/{authorId}")
    public R getBookByAuthorid(@PathVariable Long authorId){

        List<BooksVo> booksList = bookService.getBookByAuthorid(authorId);

        return R.setOk(booksList);

    }

    @GetMapping("/books/{authorId}/{bookId}")
    public R getBookByAuthoridByBookId(@PathVariable Long authorId,@PathVariable Long bookId){

        List<BooksVo> booksList = bookService.getBookByAuthoridByBookId(authorId, bookId);

        return R.setOk(booksList);

    }

    @GetMapping("/books/random/{categoryId}")
    public R getBookByCategoryIdIsRandom(@PathVariable Long categoryId){

        List<BooksVo> booksList = bookService.getBookByCategoryIdIsRandom(categoryId);

        return R.setOk(booksList);

    }


}
